#coding:utf-8
from __future__ import division

from math import ceil
class myPages:

    def __init__(self, pageCount, nowPage, countSize, urlTpl):
        self.pageCount = pageCount
        self.countSize = countSize
        self.pageSize = int(ceil(countSize / self.pageCount))
        self.urlTpl = urlTpl

      

        self.nowPage = self.checkPage(int(nowPage))
        
        p = self.nowPage-1
        self.begin = max(0, p * self.pageCount)
      
    
    def __setUrl(self, num):
        num = self.checkPage(num)
        
        
        return self.urlTpl % (num,)

    def checkPage(self, num):
        if num < 1:
            num = 1
        if num > self.pageSize:
            num = self.pageSize
       

        return num


    def createHref(self, dicts, style):
        s = ''
        if self.pageSize < 2:
            return ""
        for k in dicts:
            s = s + "<li><a  title='第%s页' href='%s'>%s</a></li>" % (self.__setUrl(dicts[k]), self.__setUrl(dicts[k]), k)

        return "<ul id=%s'>%s</ul>" % (style, s)


    def  showBase(self, style):
        lists = {"<<上一页":self.nowPage-1, "首页":1, "下一页 >> ":self.nowPage + 1, "最后一页":self.pageSize}

        return self.createHref(lists, style)











